package mybatis3.helper;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MVCSqlSessionFactory {
	private static SqlSessionFactory ssf;
	
	// mybatis-config.xml 설정파일을 읽어서
	// 마이바티스를 초기화함
	protected static SqlSessionFactory getSqlSessionFactory() {
		if (ssf == null) {
			InputStream is;
			try {
				is = Resources.getResourceAsStream("mybatis-config-mvc.xml");
				ssf = new SqlSessionFactoryBuilder().build(is);
			} catch (Exception e) {
				throw new RuntimeException(e.getCause());
			}
		}
		return ssf;
	}
	
	// 초기화된 마이바티스의 SqlSession 객체를 호출함
	public static SqlSession openSession() {
		return getSqlSessionFactory().openSession();
	}
	
}




